#include <iostream>
#include <string>
#include <pthread.h>
#include <unistd.h>

std::string ToHex(pthread_t tid)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%lx", tid);
    return id;
}

void *threadrun(void *agrs)
{
    const char *name = static_cast<const char *>(agrs);
    while (1)
    {
        std::string id = ToHex(pthread_self());
        std::cout << name << " get a tid: " << id << std::endl;
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadrun, (void *)"thread-1");

    std::cout << "new thread, tid: " << ToHex(tid) << std::endl;

    pthread_join(tid, nullptr);
    return 0;
}